/**
 * 
 */
package com.cipe.cmrs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.model.Cipeprojectstatus;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.util.DateUtil;
import com.cipe.cmrs.util.Util;

/**
 * @author Incepio
 */
@Service
@Transactional
public class CipeprojectstatusService extends BaseService<Cipeprojectstatus> {

	@Autowired
	private UserService	userService;

	/**
	 * @param displayName
	 * @return
	 */
	public Cipeprojectstatus findByDisplayName(String displayName) {
		Cipeprojectstatus cipeprojectstatus = null;
		try {
			String query = "from Cipeprojectstatus where 1=1 " + Util.createQueryClause("displayName", displayName);
			cipeprojectstatus = this.dao.getSingleResult(query);
		} catch (Exception exp) {
			logger.error(exp.getMessage(), exp);
		}
		return cipeprojectstatus;
	}

	public Cipeprojectstatus saveCipeprojectstatus(Cipeprojectstatus cipeprojectstatus, Integer userId) {
		if (cipeprojectstatus != null) {
			User user = userService.findById(userId);
			Cipeprojectstatus dbCipeprojectstatus = findById(cipeprojectstatus.getId());
			if (dbCipeprojectstatus == null) {
				dbCipeprojectstatus = new Cipeprojectstatus();
				dbCipeprojectstatus.setCreatedBy(user);
				dbCipeprojectstatus.setCreationDate(DateUtil.getCurrentDate());
			}
			dbCipeprojectstatus.setDisplayName(cipeprojectstatus.getDisplayName());
			dbCipeprojectstatus.setDescription(cipeprojectstatus.getDescription());
			dbCipeprojectstatus.setUpdatedBy(user);
			dbCipeprojectstatus.setUpdationDate(DateUtil.getCurrentDate());
			return merge(dbCipeprojectstatus);
		}
		return null;
	}
	
	/**
	 * @param displayName
	 * @return
	 */
	public List<Cipeprojectstatus> findActiveAndPendingStatuses() {
		List<Cipeprojectstatus> cipeprojectstatuses = null;
		try {
			String query = "from Cipeprojectstatus where displayName in ('Active', 'Pending')";
			
			cipeprojectstatuses = this.dao.getResultList(query);
		} catch (Exception exp) {
			logger.error(exp.getMessage(), exp);
		}
		return cipeprojectstatuses;
	}
}
